import { request } from '@/utils/request/axios';
import { ErrorMessageMode, PageRequest, PageData, AjaxResult } from '#/request';
import { MessageDtoSimple, MessageDto, MessageRequestDto, MessageReadDto } from './model';

/** 消息模块URL枚举 */
enum Api {
  /** 获取消息 */
  GetMessages = '/api/msg/getMessages',
  /** 获取未读消息 */
  GetUnreadMessages = '/api/msg/getUnreadMessages',
  /** 获取消息详情 */
  GetMessageDetails = '/api/msg/getMessageDetails',
  /** 阅读消息 */
  ReadMessages = '/api/msg/readMessages',
  /** 删除消息 */
  DeleteMessages = '/api/msg/deleteMessages',
}

/**
 * 获取消息
 * @param params 分页请求
 * @param mode 错误消息模型
 */
export function getMessages(params: PageRequest, mode: ErrorMessageMode = 'message') {
  return request.post<PageData<MessageDtoSimple>>(
    {
      url: Api.GetMessages,
      params: params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 获取未读消息
 * @param params 分页请求
 * @param mode 错误消息模型
 */
export function getUnreadMessages(params: PageRequest, mode: ErrorMessageMode = 'message') {
  return request.post<PageData<MessageDtoSimple>>(
    {
      url: Api.GetUnreadMessages,
      params: params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 获取消息详情
 * @param dto 消息请求DTO
 * @param mode 错误消息模型
 */
export function getMessageDetails(dto: MessageRequestDto, mode: ErrorMessageMode = 'message') {
  return request.post<AjaxResult<MessageDto[]>>(
    {
      url: Api.GetMessageDetails,
      params: dto,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 阅读消息
 * @param dto 消息请求DTO
 * @param mode 错误消息模型
 */
export function readMessages(dto: MessageReadDto, mode: ErrorMessageMode = 'message') {
  return request.post(
    {
      url: Api.ReadMessages,
      params: dto,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 删除消息
 * @param messageIds 消息编号
 * @param mode 错误消息模型
 */
export function deleteMessages(
  messageIds: (string | bigint)[],
  mode: ErrorMessageMode = 'message',
) {
  return request.post(
    {
      url: Api.DeleteMessages,
      params: messageIds,
    },
    {
      errorMessageMode: mode,
    },
  );
}
